/********************************************/
  // Creation History
  // --------------------------------------------------------------
  //	Company:			HTHK
  //	NAME:				StaffProjectSearch.java
  //    Title   :			find Staff Project Records, return one or more records
  //	FUNCTION:
  //	BY:					Beyes Xia
  //	DATE:				16/12/2003
  // --------------------------------------------------------------
/*********************************************/

package tss.staffproject;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.log4j.Logger;

import java.io.*;
import java.sql.*;

import com.ffpc.sql.*;
import com.ffpc.struts.StrutsMessage;
import com.util.JdbcUtil;

import tss.common.DBServlet2;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public class StaffProjectSearch
  extends DBServlet2 {
	private Logger logger = Logger.getLogger(StaffProjectSearch.class);
  @Override
protected String perform(  HttpServletRequest request,
                             HttpServletResponse response,
                             StrutsMessage message)
    throws  ServletException,
            IOException,
            SQLException,
            DataException,
            Exception
  {
            Statement statement = null;
            ResultSet resultSet = null;
            statement = this.getConnection().createStatement();
            
            String projectName = "";
            String staffName = "";
      
            if(request.getParameter("project_name") != null)
                projectName = request.getParameter("project_name");
            if(request.getParameter("staff_name") != null)
                staffName = request.getParameter("staff_name");
      //New A Staff Project
      StaffProjectBean staffProject = StaffProjectProcess.createBean();

      //Get search condition
     staffProject.setProject_name(projectName);
     staffProject.setStaff_no(staffName);

	  //Get the results
      StaffProjectBean[] staffProjects = StaffProjectProcess.find(this.getConnection(),staffProject);
      //add by Poly Li.for select the staff name//
	String staff_name[] = new String[staffProjects.length];
	try
	{
		for(int m=0;m<staffProjects.length;m++)
		{
			String sel_staffName = "select alias||' '||surname from tss.tss_c_employee";
				sel_staffName += " where staff_no='"+staffProjects[m].getStaff_no()+"'";
			resultSet = statement.executeQuery(sel_staffName);
			resultSet.next();
			staff_name[m] = resultSet.getString(1);
		}
	}
	catch(Exception ee)
	{
		logger.error(ee,ee);
	}finally{
		JdbcUtil.release(statement, resultSet);
	}
	//--------//
      //Find nothing
      if (staffProjects==null || staffProjects.length==0)
      {
          message.setMessage("No record, please input another criteria.");
          return "tss/staffproject/StaffProjectSearch.jsp";
      }
      //Find a record
      else if (staffProjects.length==1)
      {
          request.setAttribute("staffProject",staffProjects[0]);
          //request.setAttribute("staff_name",staff_name);
          return "tss/staffproject/StaffProject.jsp?formAction=Update";
      }
      //find more than one records
      else
      {
          request.setAttribute("staffProjects",staffProjects);
          request.setAttribute("staff_name",staff_name);
          return "tss/staffproject/StaffProjectList.jsp";
      }
  }
}


